package com.sl.au.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Protype;
import com.sl.au.service.ExdeCustonerInfoService;
import com.sl.utils.ExdeUtil;

@RestController
@RequestMapping(value = "/ui/CustomerTotalInfo")
public class CustomerTotalInfoContriller extends BaseController {
	@Autowired
	ExdeCustonerInfoService exdeCustonerInfoService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("chartinfo/customertotalinfo");
		return modelAndView;
	}
	
	
	/***
	 * 获取部门柱状图
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getCustomerDeptChart", method = RequestMethod.GET)
	public Map<String, Object> getDeptChart(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		//List list = exdeCustonerInfoService.getContractChartsService(1,request.getParameter("dateTimeType"),request.getParameter("beginDate"), request.getParameter("endDate"), request.getParameter("upDeptName"));
		List list = exdeCustonerInfoService.getCustomerChartsService(1, request.getParameter("beginDate"), request.getParameter("endDate"), request.getParameter("deptName"));
		List<Float> series = new ArrayList<Float>();
		List<String> Axis = new ArrayList<String>();
		for (Object row : list) {  
	        Object[] cells = (Object[]) row;
	        Axis.add(cells[0].toString());
	        series.add(Float.parseFloat(cells[1].toString()));
	    }
		map.put("Axis", Axis);
		map.put("series", series);
		return map;
	}
	/***
	 * 获取资产性质柱状图
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getZCXZChart", method = RequestMethod.GET)
	public Map<String, Object> getZCXZChart(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		//List list = exdeCustonerInfoService.getContractChartsService(1,request.getParameter("dateTimeType"),request.getParameter("beginDate"), request.getParameter("endDate"), request.getParameter("upDeptName"));
		List list = exdeCustonerInfoService.getCustomerChartsService(7, request.getParameter("beginDate"), request.getParameter("endDate"), request.getParameter("deptName"));
		List<Float> series = new ArrayList<Float>();
		List<String> Axis = new ArrayList<String>();
		for (Object row : list) {  
	        Object[] cells = (Object[]) row;
	        Axis.add(cells[0].toString());
	        series.add(Float.parseFloat(cells[1].toString()));
	    }
		map.put("Axis", Axis);
		map.put("series", series);
		return map;
	}
	/***
	 * 获取片区饼图
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getCustomerAreaChart", method = RequestMethod.GET)
	public Map<String, Object> getAreaChart(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String a = request.getParameter("beginDate");
		String b = request.getParameter("endDate");
		String c =  request.getParameter("deptName");
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
		List list = exdeCustonerInfoService.getCustomerChartsService(2, request.getParameter("beginDate"), request.getParameter("endDate"), request.getParameter("deptName"));
		List<Protype> returnList = new ArrayList<Protype>();
		for (Object row : list) {  
	        Object[] cells = (Object[]) row;
	        Protype protype = new Protype();
	        protype.setValue(Float.parseFloat(cells[1].toString()));
	        protype.setName(cells[0].toString());
	        returnList.add(protype);
	    }
		map.put("data", returnList);
		return map;
	}
	
	
	/***
	 * 获取行业饼图
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getCustomerbisChart", method = RequestMethod.GET)
	public Map<String, Object> getCustomerbisChart(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String a = request.getParameter("beginDate");
		String b = request.getParameter("endDate");
		String c =  request.getParameter("deptName");
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
		List list = exdeCustonerInfoService.getCustomerChartsService(3, request.getParameter("beginDate"), request.getParameter("endDate"), request.getParameter("deptName"));
		List<Protype> returnList = new ArrayList<Protype>();
		for (Object row : list) {  
	        Object[] cells = (Object[]) row;
	        Protype protype = new Protype();
	        protype.setValue(Float.parseFloat(cells[1].toString()));
	        protype.setName(cells[0].toString());
	        returnList.add(protype);
	    }
		map.put("data", returnList);
		return map;
	}
	/***
	 * 获取信用评分饼图
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getCustomerXYChart", method = RequestMethod.GET)
	public Map<String, Object> getCustomerXYChart(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String a = request.getParameter("beginDate");
		String b = request.getParameter("endDate");
		String c =  request.getParameter("deptName");
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
		List list = exdeCustonerInfoService.getCustomerChartsService(4, request.getParameter("beginDate"), request.getParameter("endDate"), request.getParameter("deptName"));
		List<Protype> returnList = new ArrayList<Protype>();
		for (Object row : list) {  
	        Object[] cells = (Object[]) row;
	        Protype protype = new Protype();
	        protype.setValue(Float.parseFloat(cells[1].toString()));
	        protype.setName(cells[0].toString());
	        returnList.add(protype);
	    }
		map.put("data", returnList);
		return map;
	}
	/**
	 * 统计客户数量
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getCustomerCountChart", method = RequestMethod.GET)
	public Map<String, Object> getCustomerCountChart(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String a = request.getParameter("beginDate");
		String b = request.getParameter("endDate");
		String c =  request.getParameter("deptName");
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
		List list = exdeCustonerInfoService.getCustomerChartsService(0, request.getParameter("beginDate"), request.getParameter("endDate"), request.getParameter("deptName"));
		map.put("data", list.get(0));
		return map;
	}
	/**
	 * 根据部门统计客户数量
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getDeptCustomerCountChart", method = RequestMethod.GET)
	public Map<String, Object> getDeptCustomerCountChart(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String a = request.getParameter("beginDate");
		String b = request.getParameter("endDate");
		String c =  request.getParameter("deptName");
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
		List list = exdeCustonerInfoService.getCustomerChartsService(5, request.getParameter("beginDate"), request.getParameter("endDate"), request.getParameter("deptName"));
		map.put("data", list.get(0));
		return map;
	}
	/***
	 * 获取时间趋势图
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getTrendChart", method = RequestMethod.GET)
	public Map<String, Object> getTrendChart(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		List listTotalSum= exdeCustonerInfoService.getCustomerChartsService(6,request.getParameter("beginDate"), request.getParameter("endDate"), "销售处");
		List CTlistTotalSum= exdeCustonerInfoService.getCustomerChartsService(6,request.getParameter("beginDate"), request.getParameter("endDate"), "成套处");
		List WMlistTotalSum= exdeCustonerInfoService.getCustomerChartsService(6,request.getParameter("beginDate"), request.getParameter("endDate"), "外贸处");
		List<Float> seriesSumTotal = new ArrayList<Float>();//销售处
		List<Float> CTseriesSumTotal = new ArrayList<Float>();//成套处
		List<Float> WMseriesSumTotal = new ArrayList<Float>();//外贸处
		List<String> Axis = new ArrayList<String>();//横坐标
		//销售处
		try {
			Date d1 = new SimpleDateFormat("yyyy-MM").parse(request.getParameter("beginDate"));
			Date d2 = new SimpleDateFormat("yyyy-MM").parse(request.getParameter("endDate"));//定义结束日期
			Calendar dd = Calendar.getInstance();//定义日期实例
			Calendar dd2 = Calendar.getInstance();//定义日期实例
			dd.setTime(d1);
			dd2.setTime(d2);
			dd2.add(Calendar.MONTH, 1);//进行当前日期月份加1
			while(dd.getTime().before(dd2.getTime())){//判断是否到结束日期
				boolean isFind = false;
				for(Object row : listTotalSum){
					Object[] cells = (Object[]) row;
					//System.out.println("cells[0].toString()==>"+cells[0].toString());
					//System.out.println("ExdeUtil.datechange(dd.getTime(), yyyy-MM)==>"+ExdeUtil.datechange(dd.getTime(), "yyyy-MM"));
					if(cells[0].toString().equals(ExdeUtil.datechange(dd.getTime(), "yyyy-MM"))){
						seriesSumTotal.add(Float.parseFloat(cells[1].toString()));
						//System.out.println("seriesSumTotal===>"+Float.parseFloat(cells[1].toString()));
						isFind = true;
						break;
					}
				}
				if(!isFind){
					seriesSumTotal.add(new Float(0));
				}
				//Axis.add(dd.get(Calendar.YEAR) +"年" + (dd.get(Calendar.MONTH)+1) + "月");
				//System.out.println("Axis==>"+dd.get(Calendar.YEAR) +"年" + (dd.get(Calendar.MONTH)+1) + "月");
				dd.add(Calendar.MONTH, 1);//进行当前日期月份加1
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		//成套处
		try {
			Date d1 = new SimpleDateFormat("yyyy-MM").parse(request.getParameter("beginDate"));
			Date d2 = new SimpleDateFormat("yyyy-MM").parse(request.getParameter("endDate"));//定义结束日期
			Calendar dd = Calendar.getInstance();//定义日期实例
			Calendar dd2 = Calendar.getInstance();//定义日期实例
			dd.setTime(d1);
			dd2.setTime(d2);
			dd2.add(Calendar.MONTH, 1);//进行当前日期月份加1
			while(dd.getTime().before(dd2.getTime())){//判断是否到结束日期
				boolean isFind = false;
				for(Object row : WMlistTotalSum){
					Object[] cells = (Object[]) row;
					//System.out.println("cells[0].toString()==>"+cells[0].toString());
					//System.out.println("ExdeUtil.datechange(dd.getTime(), yyyy-MM)==>"+ExdeUtil.datechange(dd.getTime(), "yyyy-MM"));
					if(cells[0].toString().equals(ExdeUtil.datechange(dd.getTime(), "yyyy-MM"))){
						WMseriesSumTotal.add(Float.parseFloat(cells[1].toString()));
						//System.out.println("seriesSumTotal===>"+Float.parseFloat(cells[1].toString()));
						isFind = true;
						break;
					}
				}
				if(!isFind){
					WMseriesSumTotal.add(new Float(0));
				}
				//Axis.add(dd.get(Calendar.YEAR) +"年" + (dd.get(Calendar.MONTH)+1) + "月");
				//System.out.println("Axis==>"+dd.get(Calendar.YEAR) +"年" + (dd.get(Calendar.MONTH)+1) + "月");
				dd.add(Calendar.MONTH, 1);//进行当前日期月份加1
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		//外贸处
		try {
			Date d1 = new SimpleDateFormat("yyyy-MM").parse(request.getParameter("beginDate"));
			Date d2 = new SimpleDateFormat("yyyy-MM").parse(request.getParameter("endDate"));//定义结束日期
			Calendar dd = Calendar.getInstance();//定义日期实例
			Calendar dd2 = Calendar.getInstance();//定义日期实例
			dd.setTime(d1);
			dd2.setTime(d2);
			dd2.add(Calendar.MONTH, 1);//进行当前日期月份加1
			while(dd.getTime().before(dd2.getTime())){//判断是否到结束日期
				boolean isFind = false;
				for(Object row : CTlistTotalSum){
					Object[] cells = (Object[]) row;
					//System.out.println("cells[0].toString()==>"+cells[0].toString());
					//System.out.println("ExdeUtil.datechange(dd.getTime(), yyyy-MM)==>"+ExdeUtil.datechange(dd.getTime(), "yyyy-MM"));
					if(cells[0].toString().equals(ExdeUtil.datechange(dd.getTime(), "yyyy-MM"))){
						CTseriesSumTotal.add(Float.parseFloat(cells[1].toString()));
						//System.out.println("seriesSumTotal===>"+Float.parseFloat(cells[1].toString()));
						isFind = true;
						break;
					}
				}
				if(!isFind){
					CTseriesSumTotal.add(new Float(0));
				}
				Axis.add(dd.get(Calendar.YEAR) +"年" + (dd.get(Calendar.MONTH)+1) + "月");
				//System.out.println("Axis==>"+dd.get(Calendar.YEAR) +"年" + (dd.get(Calendar.MONTH)+1) + "月");
				dd.add(Calendar.MONTH, 1);//进行当前日期月份加1
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		map.put("Axis", Axis);
		map.put("seriesSumTotal", seriesSumTotal);
		map.put("CTseriesSumTotal", CTseriesSumTotal);
		map.put("WMseriesSumTotal", WMseriesSumTotal);
		return map;
	}
}
